package org.wso2.carbon.registry.custom.skb.ui.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.wso2.carbon.registry.common.ui.UIException;
import org.wso2.carbon.registry.resource.ui.clients.CustomUIServiceClient;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;

public class SaveKBUtil {

    private static final Log log = LogFactory.getLog(SaveKBUtil.class);

    public static void saveKBItem(
            String path, HttpServletRequest request, ServletConfig config, HttpSession session) throws UIException {

        try {
            CustomUIServiceClient customUIServiceClient =
                    new CustomUIServiceClient(config, session);

            String problemName = request.getParameter("problemName");
            String description = request.getParameter("description");
            String link = request.getParameter("link");
            String question = request.getParameter("question");
            String answer = request.getParameter("answer");
            String solution = request.getParameter("solution");
            String keyWord = request.getParameter("keyWord");
            String product = request.getParameter("product");
            String productRelease = request.getParameter("productRelease");
            String carbonPlatform = request.getParameter("carbonPlatform");
            String jdkPlatform = request.getParameter("jdkPaltform");

            

            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMElement problem = fac.createOMElement("problem", null);
                problem.addAttribute("problemName", problemName, null);
                problem.addAttribute("description", description, null);
                problem.addAttribute("link", link, null);

            OMElement questions = fac.createOMElement("questions", null);
                questions.addAttribute("question", question,null );

            OMElement answers = fac.createOMElement("answers",null);
                answers.addAttribute("answers", answer, null);

            OMElement solutions = fac.createOMElement("solutions", null);
                solutions.addAttribute("solution", solution, null);

            OMElement keyWords = fac.createOMElement("keyWords", null);
                keyWords.addAttribute("keyWord",keyWord, null);

            OMElement products = fac.createOMElement("products", null);
                products.addAttribute("product", product, null);
                products.addAttribute("productRelease", productRelease, null);

            OMElement platform = fac.createOMElement("platform", null);
                platform.addAttribute("carbonPlatform", carbonPlatform, null);
                platform.addAttribute("jdkPlatform", jdkPlatform ,null);

            problem.addChild(questions);
            problem.addChild(answers);
            problem.addChild(solutions);
            problem.addChild(keyWords);
            problem.addChild(products);
            problem.addChild(platform);

            String content = problem.toString();

            customUIServiceClient.updateTextContent(path, content);

        } catch (Exception e) {
            String msg = "Failed to get KB C Item details. " + e.getMessage();
            log.error(msg, e);
            throw new UIException(msg, e);
        }
    }
}
